package view;

/**
 * @author Andrea
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ResourceBundle;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;


public class MenuBar {
	JMenuBar menuBar;
	JMenu menu, submenu;
	JMenuItem button;
	JCheckBoxMenuItem checkBoxButton;
	ResourceBundle myViewLabels = ResourceBundle
			.getBundle("resources.englishView");
	ResourceBundle myCommands = ResourceBundle
			.getBundle("resources.englishCommandMappings");

	JMenuBar createMenuBar() {
		// Create the menu bar
		menuBar = new JMenuBar();

		// Build the file menu.
		menu = new JMenu(myViewLabels.getString("FileMenu"));
		menuBar.add(menu);

		// Add buttons
		menu.add(makeButton(myViewLabels.getString("LoadButton"),
						KeyEvent.VK_L));
		menu.add(makeButton(myViewLabels.getString("SaveButton"),
						KeyEvent.VK_S));

		menu.addSeparator();
		menu.add(makeButton(myViewLabels.getString("QuitButton"), -1));

		// Build the actions menu
		menu = new JMenu(myViewLabels.getString("ActionsMenu"));
		menuBar.add(menu);

		// Add buttons
		menu.add(makeButton(myViewLabels.getString("StartButton"), -1));
		menu.add(makeButton(myViewLabels.getString("StopButton"), -1));

		menu.addSeparator();
		menu.add(makeButton(myViewLabels.getString("UndoButton"),
						KeyEvent.VK_Z));
		menu.add(makeButton(myViewLabels.getString("RedoButton"),
						KeyEvent.VK_Y));

		menu.addSeparator();
		menu.add(makeButton(myViewLabels.getString("ResetButton"),
				KeyEvent.VK_R));

		// Build the settings menu.
		menu = new JMenu(myViewLabels.getString("SettingsMenu"));
		menuBar.add(menu);

		// Add buttons
		menu.add(makeCheckButton(myViewLabels.getString("SoundButton")));
		menu.addSeparator();
		menu.add(makeButton(myViewLabels.getString("TurtleButton"), -1));
		menu.add(makeButton(myViewLabels.getString("BackgroundButton"), -1));
		menu.add(makeButton(myViewLabels.getString("PenButton"), -1));
		menu.addSeparator();

		// Build the zoom submenu
		submenu = new JMenu(myViewLabels.getString("ZoomButton"));
		submenu.add(makeCheckButton(myViewLabels.getString("ZoomLevel1")));
		submenu.add(makeCheckButton(myViewLabels.getString("ZoomLevel2")));
		submenu.add(makeCheckButton(myViewLabels.getString("ZoomLevel3")));
		submenu.add(makeCheckButton(myViewLabels.getString("ZoomLevel4")));
		menu.add(submenu);

		// Build help menu.
		menu = new JMenu(myViewLabels.getString("HelpMenu"));
		menuBar.add(menu);

		// Add button
		menu.add(makeButton(myViewLabels.getString("AboutButton"),
				KeyEvent.VK_F1));

		return menuBar;
	}

	// Makes a single button with its own name, action method, and possible
	// keyboard shortcut
	private JMenuItem makeButton(String name, int vk) {
		button = new JMenuItem(name);
		button.setActionCommand(name);
		if (!(vk == -1)) {
			button.setMnemonic(vk);
			button.setAccelerator(KeyStroke.getKeyStroke(vk,
					ActionEvent.CTRL_MASK));
		}
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String command = e.getActionCommand();
				Class<?> toCall = null;
				try {
					Object[] args = null;
					toCall = Class.forName(myCommands.getString(command));
					Method toInvoke = toCall.getMethod(myCommands
							.getString("MethodName"));
					Object classInstance = toCall.newInstance();
					Object doIt = toInvoke.invoke(classInstance, args);
				} catch(ClassNotFoundException ce)
				{
					System.out.println("CLASSNOTFOUND!");
					return;
				} catch (SecurityException e1) {
					System.out.println("SECURITYEXCEPTION!");
					return;
				} catch (NoSuchMethodException e2) {
					System.out.println("NOSUCHMETHODEXCEPTION!");
				} catch (InstantiationException e3) {
					System.out.println("INSTANTIATIONEXCEPTION!");
					return;
				} catch (IllegalAccessException e4) {
					System.out.println("ILLEGALACCESSEXCEPTION");
					return;
				} catch (IllegalArgumentException e5) {
					System.out.println("ILLEGALARGUMENTEXCEPTION");
					return;
				} catch (InvocationTargetException e6) {
					System.out.println("INVOCATIONTARGETEXCEPTION");
					return;
				}
			}
		});

		return button;
	}

	// Makes a checkbox button with its own name and action method
	private JCheckBoxMenuItem makeCheckButton(String name) {
		checkBoxButton = new JCheckBoxMenuItem(name);
		checkBoxButton.setActionCommand(name);
		checkBoxButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String command = e.getActionCommand();
				boolean boxChecked = checkBoxButton.isSelected();
				Class<?> toCall = null;
				try {
					Object[] args = null;
					args[0] = boxChecked;
					toCall = Class.forName(myCommands.getString(command));
					Method toInvoke = toCall.getDeclaredMethod(myCommands
							.getString("MethodName"));
					Object classInstance = toCall.newInstance();
					Object doIt = toInvoke.invoke(classInstance, args);
				} catch (Exception exc) {
					// This should never be a problem if the proper resource
					// files are in place. If it is a problem, it is not a
					// problem the user caused.
					return;
				}
			}
		});
		return checkBoxButton;
	}

}
